<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 代收结算
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="info"
                   plain
                   id="nopoint">请输入货运单编号</el-button>
        <el-input v-model="query.name"
                  placeholder="货运单编号"
                  class="handle-input mr10  selectValue"></el-input>

        <el-button type="info"
                   plain
                   id="nopoint">客户姓名</el-button>
        <el-input v-model="query.address"
                  placeholder="发货/收货客户姓名"
                  class="handle-input mr10 selectValue"></el-input>

        <el-button type="info"
                   plain
                   id="nopoint">交货时间</el-button>
        <el-date-picker v-model="value1"
                        type="datetimerange"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['8:00:00','8:00:00']"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="width:334px"
                        class="selectValue">
        </el-date-picker>

        <el-button type="primary"
                   icon="el-icon-search"
                   @click="handleSearch"
                   class="select-button">搜索</el-button>
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="handleClear">重置</el-button>
        <!-- <el-button type="primary"
                   icon="el-icon-delete"
                   class="handle-del mr10"
                   @click="delAllSelection">批量删除</el-button> -->

      </div>
      <el-table :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">

        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left"
                     inline
                     class="demo-table-expand">
              <el-form-item label="发货客户电话：">
                <span>{{ props.row.consignorPhone }}</span>
              </el-form-item>
              <el-form-item label="收货客户电话：">
                <span>{{ props.row.consigneePhone }}</span>
              </el-form-item>
              <el-form-item label="发货地址：">
                <span>{{ props.row.faddress }}</span>
              </el-form-item>
              <el-form-item label="收货地址：">
                <span>{{ props.row.saddress }}</span>
              </el-form-item>
              <el-form-item label="发货时间：">
                <span>{{ props.row.date }}</span>
              </el-form-item>
              <el-form-item label="最大贷款：">
                <span>{{ props.row.rebates }}</span>
              </el-form-item>
              <el-form-item label="佣金下限：">
                <span>10</span>
              </el-form-item>
              <el-form-item label="佣金上限：">
                <span>10000</span>
              </el-form-item>
              <el-form-item label="返款时间：">
                <span>{{ props.row.date }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>

        <el-table-column prop="waybillNumber"
                         label="货运单编号"
                         width="300"
                         align="center"></el-table-column>
        <el-table-column prop="consignorName"
                         label="发货客户"></el-table-column>
        <el-table-column prop="consigneeName"
                         label="收货客户"></el-table-column>
        <el-table-column prop="rebates"
                         label="应收贷款"></el-table-column>
        <el-table-column prop="rebates"
                         label="应收佣金(元)">
        </el-table-column>
        <el-table-column label="佣金率(%)"
                         align="center">1</el-table-column>
        <el-table-column prop="refundType"
                         label="返款业务类型(元)"
                         :formatter="refund_l"></el-table-column>
        <el-table-column prop="settlementDate"
                         label="交货时间"></el-table-column>
        <el-table-column prop="date"
                         label="返款期限"></el-table-column>

        <el-table-column label="操作"
                         width="180"
                         align="center"
                         fixed="right">
          <template slot-scope="scope">
            <el-button type="primary"
                       icon="el-icon-edit"
                       @click="handleEdit(scope.$index, scope.row)">贷款结算</el-button>
            <!-- <el-button type="danger"
                       icon="el-icon-delete"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <!-- <el-pagination background
                       layout="total, prev, pager, next"
                       :current-page="query.pageIndex"
                       :page-size="query.pageSize"
                       :total="pageTotal"
                       @current-change="handlePageChange"></el-pagination> -->

        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="currentPage2"
                       background
                       :page-sizes="[3, 5, 10, 20]"
                       :page-size="5"
                       layout="total,sizes, prev, pager, next"
                       :total="total">
        </el-pagination>

      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑"
               :visible.sync="editVisible"
               width="30%">
      <el-form ref="form"
               :model="form"
               label-width="70px">
        <el-form-item label="货运单编号">
          <el-input v-model="form.waybillNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="form.consignorName"></el-input>
        </el-form-item>
        <el-form-item label="应收货款">
          <el-input v-model="form.rebates"></el-input>
        </el-form-item>
        <el-form-item label="实收货款">
          <el-input v-model="form.rebates"></el-input>
        </el-form-item>
        <el-form-item label="本单金额">
          <el-input v-model="settleData.price"></el-input>
        </el-form-item>
        <el-form-item label="应收金额">
          <el-input v-model="settleData.amountReceivable"></el-input>
        </el-form-item>
        <el-form-item label="已收金额">
          <el-input v-model="settleData.amountReceived"></el-input>
        </el-form-item>
        <el-form-item label="预付金额">
          <el-input v-model="settleData.prepaymentAmount"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchData } from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                currentPage: '1',
                pageSize: '5',
                waybillNumber: '',
                //客户姓名
                customerName: '',
                //开始时间
                startTime: '',
                //结束时间
                endTime: ''
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},

            idx: -1,
            id: -1,
            // 时间日期数据value1
            value1: '',
            //当前页码
            currentPage2: 1,
            total: 10,
            settleData: {
                //预付金额
                prepaymentAmount: 0,
                //本单金额
                price: 0,
                //应收金额
                amountReceivable: 0,
                //已收金额
                amountReceived: 0,
                //发送后端货运单参数
                waybillNumber: '',
                freight: '',
                rabates: '',
                premium: '',
                moveCharges: '',
                settlementDate: '',
                refundType: ''
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.query.startTime = this.value1[0];
            this.query.endTime = this.value1[1];
            this.$axios.post('/api/settlement/collection/findAll', this.query).then((res) => {
                console.log(res);
                this.tableData = res.data.data.list;
                this.total = res.data.data.total;
                if (res.data.code == 200) {
                    this.$message.success('代收结算查询成功');
                } else {
                    this.$message.error('代收结算查询失败');
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            console.log(this.value1);
            this.getData();
        },
        // 清除搜索框输入
        handleClear() {
            (this.query.waybillNumber = ''), (this.query.customerName = ''), (this.value1 = '');
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    const length = this.multipleSelection.length;
                    let str = '';
                    this.delList = this.delList.concat(this.multipleSelection);
                    console.log(this.delList, '打印这个要删除的数组');
                    for (let i = 0; i < length; i++) {
                        str += this.multipleSelection[i].name + ' ';
                    }
                    this.$message.error(`删除了${str}`);
                    this.multipleSelection = [];
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.settleData.prepaymentAmount = this.form.premium + this.form.moveCharges + this.form.freight;
            this.settleData.price = this.settleData.prepaymentAmount;
            this.settleData.amountReceivable = this.settleData.price - this.settleData.prepaymentAmount;

            this.settleData.waybillNumber = this.form.waybillNumber;
            this.settleData.freight = this.form.freight;
            this.settleData.rebates = this.form.rebates;
            this.settleData.premium = this.form.premium;
            this.settleData.moveCharges = this.form.moveCharges;

            this.settleData.settlementDate = this.form.settlementDate;
            this.settleData.refundType = this.form.refundType;

            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            console.log(this.settleData);
            // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            console.log(this.form);
            this.settle();
            // this.getData();
            this.$set(this.tableData, this.idx, this.form);
        },
        //货运单结算操作
        settle() {
            this.$axios.post('/api/settlement/collection/settle', this.settleData).then((res) => {
                // console.log(this.settleData,"当前发送的对象");
                if (res.data.code == 200) {
                    this.$message.success('结算成功');
                } else {
                    this.$message.error('结算失败');
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.query.pageSize = val;
            this.getData();
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.query.currentPage = val;
            console.log();
            this.getData();
        },
        refund_l(row, column) {
            if (row.refundType === '1') {
                return '单日返';
            } else {
                return '无';
            }
        }
    }
    // dateafter(row,column){

    // },
    // recived(row,column){

    // }
};
</script>


<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-select {
    margin-left: 10px;
    width: 100px;
}
/* 按钮不展示悬浮鼠标效果 */
#nopoint {
    pointer-events: none;
}
/* 修改时间选择框长度 */
.handle-input {
    width: 150px;
    display: inline-block;
}
/* 搜索按钮间距设置 */
.select-button {
    margin-left: 10px;
}
/* 二级表单样式 */
.demo-table-expand {
    font-size: 0;
}
.demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}
.demo-table-expand .el-form-item {
    margin-left: 5%;
    margin-right: 0;
    margin-bottom: 0;
    width: 40%;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
